package com.faxsun.controller.account;

import com.faxsun.core.order.domain.FSOrderItemImpl;
import com.faxsun.core.order.service.FSOrderItemServiceImpl;
import com.faxsun.profile.core.domain.FSReferralCustomerOrderItemXref;
import com.faxsun.profile.core.domain.FSReferralStatusEnum;
import com.faxsun.web.processor.tool.CheckMobile;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.broadleafcommerce.core.order.domain.OrderItem;
import org.broadleafcommerce.core.order.service.OrderItemService;
import org.broadleafcommerce.profile.core.domain.Customer;
import org.broadleafcommerce.profile.core.service.CustomerService;
import org.broadleafcommerce.profile.web.core.CustomerState;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Rongxin on 2015-07-15.
 */
@Controller
@RequestMapping("/account/recommend")
public class RecommendController {

    protected static final Log LOG = LogFactory.getLog(RecommendController.class);
    protected static String recommendHistoryView = "account/recommendHistory";

    @Resource(name = "blOrderItemService")
    protected OrderItemService orderItemService;

    @RequestMapping(method = RequestMethod.GET)
    public String viewRecommendHistotry(HttpServletRequest request, Model model) {
        Customer customer = CustomerState.getCustomer();
        List<FSReferralCustomerOrderItemXref> referralOrderItems =
            ((FSOrderItemServiceImpl) orderItemService).readReferralOrderItemsByCustomer(customer);

        Map<FSReferralStatusEnum, BigDecimal> referralCreditSummary = new HashMap<FSReferralStatusEnum, BigDecimal>();
        for (FSReferralCustomerOrderItemXref referralOrderItem : referralOrderItems) {
            FSReferralStatusEnum referralStatus = referralOrderItem.getReferralStatus();
            if (!referralCreditSummary.containsKey(referralStatus)) {
                referralCreditSummary.put(referralStatus, BigDecimal.ZERO);
            }
            referralCreditSummary.put(
                referralStatus, referralCreditSummary.get(referralStatus).add(referralOrderItem.getReferralCredit()));
        }

        BigDecimal addedTotal = referralCreditSummary.containsKey((FSReferralStatusEnum.ADDED)) ?
            referralCreditSummary.get(FSReferralStatusEnum.ADDED) : BigDecimal.ZERO;
        BigDecimal confirmedTotal = referralCreditSummary.containsKey((FSReferralStatusEnum.CONFIRMED)) ?
            referralCreditSummary.get(FSReferralStatusEnum.CONFIRMED) : BigDecimal.ZERO;
        BigDecimal pendingTotal = referralCreditSummary.containsKey((FSReferralStatusEnum.PENDDING)) ?
            referralCreditSummary.get(FSReferralStatusEnum.PENDDING) : BigDecimal.ZERO;
        BigDecimal rebatedTotal = referralCreditSummary.containsKey((FSReferralStatusEnum.REBATED)) ?
            referralCreditSummary.get(FSReferralStatusEnum.REBATED) : BigDecimal.ZERO;
        // BigDecimal creditTotal = confirmedTotal.add(pendingTotal).add(rebatedTotal);

        // 已添加金额
        model.addAttribute("addedTotal", addedTotal);
        // 可提取金额
        model.addAttribute("confirmedTotal", confirmedTotal);
        // 未确认金额
        model.addAttribute("pendingTotal", pendingTotal);
        // 已提取金额
        model.addAttribute("rebatedTotal", rebatedTotal);
        model.addAttribute("referralOrderItems", referralOrderItems);
        model.addAttribute("isMobile", CheckMobile.check(request.getHeader("user-agent")));

        return getRecommendHistoryView();
    }

    private String getRecommendHistoryView() {
        return recommendHistoryView;
    }

}
